﻿//*** Guidance generated code ***//
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Web.UI;

using ProjectBase.Core;
using ProjectBase.Core.Web;

namespace $safeprojectname$
{
    /// <summary>
    ///     Generic base for childless ContentControls (without ascx).
    ///     These should have Begin and End tags (e.g. Label, Legend).
    ///     This base introduces fluent attribute setting.
    /// </summary>
    /// <typeparam name = "TContentControl"></typeparam>
    /// <typeparam name = "TModel"></typeparam>
    public abstract class ContentControl<TModel, TContentControl> : VisualControl<TModel>
        where TModel : class, ICoreModel
        where TContentControl : ContentControl<TModel, TContentControl>
    {
        #region Constructor
        protected ContentControl(string cssClassName = null)
        {
            base.SetAttribute(Attr.Class, cssClassName);
        }
        #endregion Constructor

        #region Set Attributes
        public TContentControl SetAttribute(string attributeName, string value)
        {
            base.SetAttribute(attributeName, value);
            return this as TContentControl;
        }

        /// <summary>
        ///     Appends value to existing attribute (or creates new).
        ///     Attention: CSS class names must be separated by spaces in the "value" paramater.
        /// </summary>
        /// <param name = "attributeName"></param>
        /// <param name = "value"></param>
        /// <returns></returns>
        public virtual TContentControl ExtendAttribute(string attributeName, string value)
        {
            if (attributeName.IsNotEmpty()
                && value.Is())
            {
                Attributes[attributeName] = (Attributes[attributeName] ?? string.Empty)
                                            + value;
            }
            return this as TContentControl;
        }

        /// <summary>
        ///     Appends the value to the existing "class" attribute (or creates new one).
        ///     the space " " separator is also injected
        /// </summary>
        /// <param name = "cssClassName"></param>
        /// <returns></returns>
        public TContentControl AddCssClassName(string cssClassName)
        {
            if (cssClassName.IsNotEmpty())
            {
                return ExtendAttribute(Attr.Class, HtmlTextWriter.SpaceChar + cssClassName);
            }
            return this as TContentControl;
        }

        public virtual TContentControl SetCssClassName(string cssClassName)
        {
            if (cssClassName.IsNotEmpty())
            {
                SetAttribute(Attr.Class, cssClassName);
            }
            return this as TContentControl;
        }

        public virtual TContentControl SetTitle(string title)
        {
            if (title.IsNotEmpty())
            {
                SetAttribute(Attr.Title, title);
            }
            return this as TContentControl;
        }
        public virtual TContentControl SetId(string id)
        {
            if (id.IsNotEmpty())
            {
                SetAttribute(Attr.ID, id);
            }
            return this as TContentControl;
        }
        #endregion Set Attributes
    }

    /// <summary>
    ///     Generic base for Controls with children (without ascx).
    ///     TChildControl interface allows to restrict the child type
    ///     (E.g. Table can contain only ITableChild).
    ///     This base introduces the fluent child control extending.
    /// </summary>
    /// <typeparam name = "TChildControl"></typeparam>
    /// <typeparam name = "TContentControl"></typeparam>
    /// <typeparam name = "TModel"></typeparam>
    public abstract class ContentControl<TModel, TChildControl, TContentControl> : ContentControl<TModel, TContentControl>, IEnumerable
        where TModel : class, ICoreModel
        where TContentControl : ContentControl<TModel, TChildControl, TContentControl>
        where TChildControl : IVisualControl
    {
        #region Constructor
        protected ContentControl(string cssClassName = null) : base(cssClassName) { }
        #endregion Constructor

        #region Add Controls
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
        protected virtual IEnumerator GetEnumerator()
        {
            if (Controls.Is())
            {
                return Controls.GetEnumerator();
            }
            return null;
        }

        public virtual TContentControl AddControls(params TChildControl[] controls)
        {
            foreach (var control in controls)
            {
                Add(control);
            }
            return this as TContentControl;
        }

        public virtual void Add(TChildControl control)
        {
            Controls.Add(control);
        }
        #endregion

        public override TModel Model { get { return base.Model; } } // code contracts suggestion
    }
}